﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchemaMapper;

namespace TX.CodeSmith
{
    public class ControlHelper
    {
        public static string GetTypesName(Type p)
        {
            string name = p.Name;
            if (p == typeof(Int16) ||
                p == typeof(Int32) ||
                p == typeof(Int64) ||
                p == typeof(byte))
            {

                name = "整数";
            }
            else if (p == typeof(decimal) ||
               p == typeof(float) ||
               p == typeof(double))
            {

                name = "数字";
            }
            return name;
        }
        public static string GetItemControlType(Property p)
        {
            string controltype = "Framework.WindowUI.Controls.TXTextBox";
            if (p.SystemType == typeof(DateTime) || p.SystemType == typeof(Nullable<DateTime>))
            {
                controltype = "Framework.WindowUI.Controls.TXDateTimePicker";
            }
            else if (p.SystemType == typeof(Boolean) || p.SystemType == typeof(Nullable<Boolean>))
            {
                controltype = "Framework.WindowUI.Controls.TXCheckBox";
            }

            return controltype;
        }

        public static string GetTemplateFormatStr(Property p)
        {
            string str = string.Empty;
            if (p.SystemType == typeof(string))
            {
                str = string.Format("$this.{0}", p.PropertyName.ToSafeNameNoTrimS());
            }
            else if (p.SystemType == typeof(Int16) ||
                          p.SystemType == typeof(Nullable<Int16>) ||
                          p.SystemType == typeof(Int32) ||
                          p.SystemType == typeof(Nullable<Int32>) ||
                          p.SystemType == typeof(Int64) ||
                          p.SystemType == typeof(Nullable<Int64>) ||
                          p.SystemType == typeof(Byte) ||
                          p.SystemType == typeof(Nullable<Byte>) ||
                          p.SystemType == typeof(byte)
                    )
            {
                str = string.Format("$this.{0}", p.PropertyName.ToSafeNameNoTrimS());
            }
            else if (p.SystemType == typeof(Decimal) ||
                      p.SystemType == typeof(Nullable<Decimal>) ||
                      p.SystemType == typeof(float) ||
                      p.SystemType == typeof(Nullable<float>) ||
                      p.SystemType == typeof(Double) ||
                      p.SystemType == typeof(Nullable<Double>))
            {
                str = string.Format("$this.{0}.ToString(\\\"F2\\\")", p.PropertyName.ToSafeNameNoTrimS());
            }
            else if (p.IsNullable.Value && p.SystemType == typeof(Boolean))
            {
                str = string.Format("#if($this.{0}) 是 #else 否 #end", p.PropertyName.ToSafeNameNoTrimS());
            }
            else if (p.SystemType == typeof(DateTime) || p.SystemType == typeof(Nullable<DateTime>))
            {
                str = string.Concat("${this.", p.PropertyName.ToSafeNameNoTrimS(), ".ToString(\\\"yy-MM-dd hh:mm\\\")}");
            }

            return str;

        }

        /// <summary>
        /// 获取实体对象属性值的格式化表达式
        /// </summary>
        /// <param name="p">The p.</param>
        /// <returns>Return a data(or instance) of String.</returns>
        /// User:Ryan  CreateTime:2012-9-18 13:09.
        public static string GetItemValueFormatStr(Property p, string subfix)
        {
            if (p.SystemType == typeof(string))
            {
                return "TX.Framework.Helper.Convert.ToString(" + subfix + "." + p.PropertyName.ToSafeNameNoTrimS() + ")";
            }
            else if ( p.SystemType == typeof( DateTime ) )
            {
                return "TX.Framework.Helper.Convert.ToDateTime(" + subfix + "." + p.PropertyName.ToSafeNameNoTrimS() + ")";
            }
            else if (!p.IsNullable.Value &&
                         (p.SystemType == typeof(Int16) ||
                         p.SystemType == typeof(Int32) ||
                         p.SystemType == typeof(Int64) ||
                         p.SystemType == typeof(Decimal) ||
                         p.SystemType == typeof(float) ||
                         p.SystemType == typeof(Double) ||
                         p.SystemType == typeof(Byte))
                   )
            {
                return subfix + "." + p.PropertyName.ToSafeNameNoTrimS() + ".ToString()";

            }
            else if (p.IsNullable.Value &&
                         (p.SystemType == typeof(Int16) ||
                         p.SystemType == typeof(Int32) ||
                         p.SystemType == typeof(Int64) ||
                         p.SystemType == typeof(Decimal) ||
                         p.SystemType == typeof(float) ||
                         p.SystemType == typeof(Double) ||
                         p.SystemType == typeof(Byte))
                   )
            {
                return "(" + subfix + "." + p.PropertyName.ToSafeNameNoTrimS() + "==null?\"\":" + subfix + "." + p.PropertyName.ToSafeNameNoTrimS() + ".Value.ToString())";

            }
            else if (!p.IsNullable.Value && p.SystemType == typeof(Boolean))
            {
                return subfix + "." + p.PropertyName.ToSafeNameNoTrimS();
            }
            else if (p.SystemType == typeof(Boolean))
            {
                return "(" + subfix + "." + p.PropertyName.ToSafeNameNoTrimS() + "==null?false:" + subfix + "." + p.PropertyName.ToSafeNameNoTrimS() + ".Value)";
            }
            else
            {
                return subfix + "." + p.PropertyName.ToSafeNameNoTrimS();
            }
        }

        public static string GetItemControlName(Property p)
        {
            string controlName = "txt";
            if (p.SystemType == typeof(DateTime) || p.SystemType == typeof(Nullable<DateTime>))
            {
                controlName = "dt";
            }
            else if (p.SystemType == typeof(Boolean) || p.SystemType == typeof(Nullable<Boolean>))
            {
                controlName = "chb";
            }

            return string.Concat(controlName, p.PropertyName);
        }

        public static string GetItemControlValueProperty(Property p)
        {
            string controltype = "Text";
            if (p.SystemType == typeof(DateTime) || p.SystemType == typeof(Nullable<DateTime>))
            {
                controltype = "Value";
            }
            else if (p.SystemType == typeof(Boolean) || p.SystemType == typeof(Nullable<Boolean>))
            {
                controltype = "Checked";
                //controltype = "Checked?\"True\":\"False\"";
            }

            return controltype;
        }

        public static string GetSqlMethodByType(Type t)
        {
            string result = "Contains";
            if (t == typeof(Int16) ||
                t == typeof(Int32) ||
                t == typeof(Int64) ||
                t == typeof(Byte) ||
                t == typeof( Boolean ) ||
                t == typeof( Decimal ) ||
                t == typeof(Guid) ||
                t == typeof(Nullable<Int16>) ||
                t == typeof(Nullable<Int32>) ||
                t == typeof(Nullable<Int64>) ||
                t == typeof( Nullable<Byte> ) ||
                t == typeof( Nullable<Decimal> ) ||
                t == typeof(Nullable<Guid>) ||
                t == typeof(Nullable<Boolean>))
            {
                result = "Equals";
            }
            return result;
        }

        public static string GetMethodByType( Type t,string obj )
        {
            string result = ".Contains("+obj+")";
            if ( t == typeof( Int16 ) ||
                t == typeof( Int32 ) ||
                t == typeof( Int64 ) ||
                t == typeof( Byte ) ||
                t == typeof( Boolean ) ||
                t == typeof( Decimal ) ||
                t == typeof( Guid ) ||
                t == typeof( Nullable<Int16> ) ||
                t == typeof( Nullable<Int32> ) ||
                t == typeof( Nullable<Int64> ) ||
                t == typeof( Nullable<Byte> ) ||
                t == typeof( Nullable<Guid> ) ||
                t == typeof( Nullable<Decimal> ) ||
                t == typeof( Nullable<Boolean> ) )
            {
                result = "==" + obj;
            }
            return result;
        }

        public static string GetNullAbleType( Property p )
        {
            if ( p.SystemType.IsValueType)
            {
               return p.SystemType.Name+"?";
            }
            else
            {
                return p.SystemType.Name;
            }
        }
    }
}
